package com.thenie.service.impl;


import com.thenie.dao.BookDao;
import com.thenie.dao.impl.BookDaoImpl;
import com.thenie.pojo.Book;
import com.thenie.pojo.Page;
import com.thenie.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {
    private BookDao bookDao=new BookDaoImpl();

    @Override
    public int addBook(Book book) {
        return bookDao.addBook(book);
    }

    @Override
    public int deleteBookById(Integer id) {
        return bookDao.deleteBookById(id);
    }

    @Override
    public int updateBook(Book book) {
        return bookDao.updateBook(book);
    }

    @Override
    public Book queryBook(Integer id) {
        return bookDao.queryBook(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        int  pageTotal;
        int pageTotalCount;
        List<Book> items;
        pageTotalCount=bookDao.queryPageTotalCount();
        pageTotal=pageTotalCount/pageSize;
        if( pageTotalCount % pageSize>0){
            pageTotal++;
        }
        if (pageNo<1||pageNo>pageTotal){
            pageNo=1;
        }
        int begin=(pageNo-1)*pageSize;
        items=bookDao.queryPageItems(begin, pageSize);

        Page<Book> bookPage = new Page<Book>(pageNo,pageTotal, pageTotalCount, items, pageSize);
        return bookPage;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        int  pageTotal;
        int pageTotalCount;
        List<Book> items;
        pageTotalCount=bookDao.queryPageTotalCountOnPrice(min,max);
        pageTotal=pageTotalCount/pageSize;
        if( pageTotalCount % pageSize>0){
            pageTotal++;
        }
        if (pageNo<1||pageNo>pageTotal){
            pageNo=1;
        }
        int begin=(pageNo-1)*pageSize;
        items=bookDao.queryPageItemsOnPrice(begin, pageSize, min, max);

        Page<Book> bookPage = new Page<Book>(pageNo,pageTotal, pageTotalCount, items, pageSize);
        return bookPage;
    }


}
